package com.ficus.road.maintain.third.zh.coverage;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.coverage.UnitCoverageWeek;
import com.ficus.road.maintain.core.model.coverage.UnitCoverageWeekZH;
import com.ficus.road.maintain.dao.coverage.UnitCoverageWeekDao;
import com.ficus.road.maintain.service.CycleNumService;
import com.ficus.road.maintain.third.zh.BaseZH;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/3/16 下午4:03
 */
@Component
public class ZHUnitCoverageWeek extends BaseZH {

    @Autowired
    private UnitCoverageWeekDao unitCoverageWeekDao;
    @Autowired
    private CycleNumService cycleNumService;

    @PostConstruct
    void init() {
        arg0 = "ytkjSectionWeekCover";
    }

    //  @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0/10 6-22 * * ?", zone = "GMT+8:00")
    public synchronized void refreshUnitCoverageWeek() {
        //  更新本周期覆盖率
        arg1 = "{}";
        errMsg = "调用中海接口获取标段本周期覆盖率异常！";
        successMsg = "调用中海接口获取标段本周期覆盖率成功！";
        String unitCoverageStr = null;
        int i = 0;
        while (i < 10) {
            unitCoverageStr = getZHData();
            if (ObjectUtils.isNotEmpty(unitCoverageStr)) {
                break;
            }
            i++;
        }
        if (ObjectUtils.isEmpty(unitCoverageStr)) {
            return;
        }
        Date today = new Date();
        List<UnitCoverageWeekZH> unitCoverageWeekZHList = JSONArray.parseArray(unitCoverageStr, UnitCoverageWeekZH.class);
        List<UnitCoverageWeek> unitCoverageWeekList = unitCoverageWeekZHList.stream().map(item -> item.convert2UnitCoverage(item, today)).collect(Collectors.toList());
        unitCoverageWeekList.forEach(unitCoverageWeek -> {
            unitCoverageWeekDao.saveOrUpdate(unitCoverageWeek);
        });
    }

    //    @Scheduled(cron = "*/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0/30 1-4 * * ?", zone = "GMT+8:00")
    public synchronized void refreshUnitCoverageLastWeek() {
        //  更新上周期覆盖率（中海半夜补录）
        String currentCycleNum = cycleNumService.getCurrentCycleNum();
        String lastCycleNum = cycleNumService.getLastCycleNum(currentCycleNum);
        arg1 = "{\"cyclenum\": \"" + lastCycleNum + "\"}";
        errMsg = "调用中海接口获取标段上周期覆盖率异常！";
        successMsg = "调用中海接口获取标段上周期覆盖率成功！";
        String unitCoverageStr = getZHData();
        if (ObjectUtils.isEmpty(unitCoverageStr)) {
            return;
        }
        Date today = new Date();
        List<UnitCoverageWeekZH> unitCoverageWeekZHList = JSONArray.parseArray(unitCoverageStr, UnitCoverageWeekZH.class);
        List<UnitCoverageWeek> unitCoverageWeekList = unitCoverageWeekZHList.stream().map(item -> item.convert2UnitCoverage(item, today)).collect(Collectors.toList());
        unitCoverageWeekList.forEach(unitCoverageWeek -> {
            unitCoverageWeekDao.saveOrUpdate(unitCoverageWeek);
        });
    }
}
